package com.telnetnz.ticketdesk.bean;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;

import com.rockbb.common.utilities.ObjectUtils;
import com.telnetnz.ticketdesk.bean.misc.ComparableObject;
import com.telnetnz.ticketdesk.bean.misc.OperationLogObject;
import com.telnetnz.ticketdesk.dao.entity.ProjectEntity;
import com.telnetnz.ticketdesk.service.OperationLogService;
import com.telnetnz.ticketdesk.service.ProjectService;
import com.telnetnz.ticketdesk.service.ProjectToUserService;
import com.telnetnz.ticketdesk.service.UserService;

public class Project extends ProjectEntity implements OperationLogObject,
		ComparableObject {

	private static final long serialVersionUID = 822466348039836270L;

	public List<ProjectToUser> getProjectUsers() {
		return ProjectToUserService.getByProjectId(getId());
	}

	public List<OperationLog> getOperationLogs() {
		return OperationLogService.getLogsByPageAndCondition(
				0, 
				10, 
				OperationLogService.ORDERBY_TIME, 
				0, 
				0L, 		// userId
				OperationLogService.OBJECT_TYPE_PROJECT, // objectType
				getId(), 
				null, 
				null, 
				null);
	}

	public static Project adapt(ProjectEntity entity) {
		if (entity == null)
			return null;
		Project bean = new Project();
		BeanUtils.copyProperties(entity, bean);
		return bean;
	}

	public ProjectEntity toEntity() {
		return this;
	}

	public User getCreator() {
		return UserService.getUser(getCreatorId());
	}

	public User getUpdater() {
		return UserService.getUser(getUpdaterId());
	}

	public Map<String, List<String>> compare() {
		Project a = ProjectService.getRawProject(getId());
		return ObjectUtils.compare(a.toEntity(), this.toEntity(), ProjectEntity.class);
	}

	public String getObjectName() {
		return this.getName();
	}

	public String getObjectSummary() {
		return this.getDescription();
	}
}